//
//  AllMovieDetailViewController.m
//  newDouban
//
//  Created by dllo on 15/10/27.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "AllMovieDetailViewController.h"
#import "AllMovieDetailView.h"
#import "AppTools.h"
#import "UIImageView+WebCache.h"
#import "MovieDetail.h"
#import "LoginViewController.h"
#import "MyMovieViewController.h"
#import "SingleTon.h"
#import "MovieDataSingTon.h"

@interface AllMovieDetailViewController ()

@property (nonatomic, retain) MovieDetail *movieDetail;
@property (nonatomic, copy) NSString *str;
@end

@implementation AllMovieDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    //self.navigationItem.title = @"电影详情";
    
    AllMovieDetailView *allMovieDTV = [[AllMovieDetailView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:allMovieDTV];
    [allMovieDTV release];
    
    [self openDB];
    
    NSString *urlStr = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchmovie.php?movieId=%@", self.movieID];
    [AppTools getDataWithURL:urlStr block:^(id result) {
        
        [self dropTable];
        [self createTable];
        
        NSMutableDictionary *dic = result;
        NSDictionary *resultDic = [dic objectForKey:@"result"];
        self.movieDetail = [[MovieDetail alloc] init];
        [self.movieDetail setValuesForKeysWithDictionary:resultDic];
        //[self insertMovie:self.movieDetail];
        allMovieDTV.movieDt = self.movieDetail;
        [_movieDetail release];
        
        self.navigationItem.title = [resultDic objectForKey:@"title"];
        NSURL *url = [NSURL URLWithString:[resultDic objectForKey:@"poster"]];
        [allMovieDTV.movielcon sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"picholder.png"]];
        
    }];
    
    //allMovieDTV.movieDt = self.movieDetail;
    
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_back@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction:)] autorelease];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_share@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonAction:)] autorelease];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    
}

//打开数据
- (void)openDB
{
    [[MovieDataSingTon shareMovieDataBase] openDBDT];
}

//创建表
- (void)createTable
{
    [[MovieDataSingTon shareMovieDataBase] createTableDT];
}
//插入数据
- (void)insertMovie:(MovieDetail *)movie
{
    [[MovieDataSingTon shareMovieDataBase] insertMovieDT:movie];
}
//查询数据
- (NSMutableArray *)selectMovie
{
    NSMutableArray *arrM = [[MovieDataSingTon shareMovieDataBase] selectMovieDT];
    
    return arrM;
    
}
//删除表
- (void)dropTable
{
    [[MovieDataSingTon shareMovieDataBase] dropTableDT];
}


- (void)UIAlertController
{
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
    //注意:假如下面要加textfield要用UIAlertControllerStyleAlert
    
    [self presentViewController:alertC animated:YES completion:^{
        
        
    }];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //block回调
        NSLog(@"点击的是确定按钮");
        [self.navigationController popToRootViewControllerAnimated:YES];
        
    }];
    
    //    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    //        //block回调
    //        NSLog(@"点击的是取消按钮");
    //
    //    }];
    //将按钮放在提示框上
    [alertC addAction:action];
    //    [alertC addAction:action1];
    
}

- (void)leftBarButtonAction:(UIBarButtonItem *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)rightBarButtonAction:(UIBarButtonItem *)sender
{
    NSString *isLogin = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName1"];
    
    if (isLogin.length > 0) {
        
            MyMovieViewController *myMovieVC = [[MyMovieViewController alloc] init];
            myMovieVC.myTitle = self.movieDetail.title;
            myMovieVC.movieDetail = self.movieDetail;
            myMovieVC.movieID = self.movieID;
            NSLog(@"self.movieID ===== %@", self.movieDetail.title);
            [self.navigationController pushViewController:myMovieVC animated:YES];
            [self UIAlertController];
        
    } else {
        
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        UINavigationController *loginNC = [[UINavigationController alloc] initWithRootViewController:loginVC];
        [self presentViewController:loginNC animated:YES completion:^{
            
        }];
        
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
